package it.polimi.traveldream.web.beans;

import it.polimi.traveldream.ejb.management.UserDTO;
import it.polimi.traveldream.ejb.management.UserMgr;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.apache.commons.codec.digest.DigestUtils;

@ManagedBean(name="profileBean")
@RequestScoped
public class ProfileBean {
	
	@EJB
	private UserMgr userMgr;
	
	private UserDTO user;
	
	@PostConstruct
	public void init() {
		user = userMgr.getUserDTO();
	}//-----
	
	public UserDTO getUser(){
		return this.user;
	}
	
	public void setUser(UserDTO user) {
		this.user = user;
	}
	
	public String profile() {
		return "/profiloPersonale?faces-redirect=true";
	}
	
	
	/*public String modMail() {
		//userMgr.update(user);
		//creare userDTO e,,,,
		return "/modMail?faces-redirect=true";
	}*/
	
	public String modPass() {
		return "/pagine_clienti/modPass?faces-redirect=true";
	}
	
	public String modPassImp() {
		return "/pagine_impiegati/modPass?faces-redirect=true";
	}
	
	public String modificaCli(){
		user.setPassword(DigestUtils.sha512Hex(user.getPassword()));
		userMgr.updatePassword(user);
		
		return "/pagine_clienti/modPassOk?faces-redirect=true";
	}
	
	public String modificaImp() {
		user.setPassword(DigestUtils.sha512Hex(user.getPassword()));
		userMgr.updatePassword(user);
		
		return "/pagine_impiegati/modPassOk?faces-redirect=true";
		
	}
	
	public String goToHome() {
		return "home?faces-redirect=true";
	}
}


